package com.joker.aigc.oj.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.joker.aigc.oj.domain.entity.Recruit;
import com.joker.aigc.oj.domain.entity.RecruitApply;
import com.joker.aigc.oj.mapper.RecruitApplicationMapper;
import com.joker.aigc.oj.mapper.RecruitMapper;
import com.joker.aigc.oj.service.RecruitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RecruitServiceImpl extends ServiceImpl<RecruitMapper, Recruit> implements RecruitService {

    @Autowired
    RecruitApplicationMapper recruitApplicationMapper;

    @Override
    public IPage<Recruit> selectAllRecruit(Integer current) {
        Page<Recruit> c = new Page<>(current, 10);


        return this.baseMapper.selectPage(c, null);
    }

    @Override
    public Recruit selectRecruitByName(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public Integer addRecruit(Recruit recruit) {

//         首先检查申请的岗位是否存在
        RecruitApply recruitApplication = recruitApplicationMapper.selectById(recruit.getRaId());

        if (recruitApplication == null) {
            return -2;
        }
        recruit.setPass(0);
        recruit.setPassWrite(0);
        recruit.setPassMeeting(0);
        recruit.setStep(1);
        recruit.setContract(0);
        recruit.setMeetingScore(0.0);
        recruit.setWriteScore(0.0);
        recruit.setScore(0.0);


        return this.baseMapper.insert(recruit);

    }

    @Override
    public Integer updateRecruit(Recruit recruit) {
        return null;
    }

    @Override
    public Integer pass(List<Long> ids) {
        Integer results = 0;
        for (Long id : ids) {

            Recruit recruit = this.baseMapper.selectById(id);

            if (recruit == null) {
                continue;
            }

            recruit.setStep(2); // 进行到第二部
            recruit.setPass(1); // 这里给了笔试或者面试机会

            results += this.baseMapper.updateById(recruit);
        }
        return results;

    }
}
